/**
 * 数据结构：
 *       Section
 *        /   \
 *      Heap  Heap ...
 *      / \    / \
 * Range Range ...
 */
#include <stdlib.h>
#include <stdio.h>

typedef enum {
  false = 0,
  true = !false
} bool;

/**
 * 单个区间。
 */
typedef struct _Range {
  int begin;
  int end;
  struct _Range* next; // 单链表结构
} *Range;

/**
 * 区间构造器。
 */
Range range_create(int index) {
  Range this = (Range)calloc(1, sizeof(struct _Range));
  this->begin = this->end = index;
  this->next = NULL;
  return this;
}

/**
 * 区间析构器。
 */
void range_delete(Range this) {
  free(this);
}

/**
 * 区间是否为空。
 */
bool range_is_empty(Range this) {
  return this->begin > this->end;
}

/**
 * 区间扩张。
 */
void range_push(Range this, int index) {
  this->end = index;
}

/**
 * 弹出最左边的数字。
 */
int range_pop(Range this) {
  int index = this->begin;
  this->begin++;
  return index;
}

/**
 * 多个区间合并而成的堆。
 */
typedef struct _Heap {
  int value;
  int length;
  Range head;
  Range tail;
  struct _Heap* next;
} *Heap;

/**
 * 堆构造器。
 */
Heap heap_create(int value, int index) {
  Heap this = (Heap)calloc(1, sizeof(struct _Heap));
  this->value = value;
  this->length = 1;
  this->head = this->tail = range_create(index);
  this->next = NULL;
  return this;
}

/**
 * 堆析构器。
 */
void heap_delete(Heap this) {
  Range range = this->head;
  while (range != NULL) {
    Range current = range;
    range = range->next;
    range_delete(current);
  }
  free(this);
}

bool heap_is_empty(Heap this) {
  return this->length == 0;
}

/**
 * 往堆末尾追加元素。
 * 堆的长度加1。
 */
void heap_push(Heap this, int index) {
  this->length++;
  range_push(this->tail, index);
}

/**
 * 做最左边的区间弹出最左边的数字。
 * 若最左边的区间为空，则删除该区间。
 */
int heap_pop(Heap this) {
  this->length--;
  int index = range_pop(this->head);
  if (range_is_empty(this->head)) {
    this->head = this->head->next;
  }
  return index;
}

/**
 * 将第二个堆（that）合并到第一个堆（this）中。
 * 同时释放掉第二个堆。
 */
void heap_merge(Heap this, Heap that) {
  this->length += that->length;
  this->tail->next = that->head;
  this->tail = that->tail;
  that->head = that->tail = NULL;
  heap_delete(that);
}

/**
 * 序列：保存整个水果序列。
 */
typedef struct _Sequence {
  int limit; // 水果的总数。
  int size; // 堆的数量。
  int minimalLength; // 堆最小的长度。
  Heap head;
  Heap tail;
} *Sequence;

/**
 * 序列构造器。
 */
Sequence sequence_create() {
  Sequence this = (Sequence)calloc(1, sizeof(struct _Sequence));
  this->size = 0;
  this->minimalLength = 0;
  this->head = this->tail = NULL;
  return this;
}

/**
 * 序列析构器。
 */
void sequence_free(Sequence this) {
  Heap heap = this->head;
  while (heap != NULL) {
    Heap current = heap;
    heap = heap->next;
    heap_delete(current);
  }
  free(this);
}

/**
 * 序列是否为空。
 */
bool sequence_is_empty(Sequence this) {
  return this->size == 0;
}

/**
 * 往序列末尾追加数据。
 */
void sequence_push(Sequence this, int index, int value) {
  if (this->tail != NULL) {
    if (this->tail->value == value) {
      heap_push(this->tail, index);
    } else {
      this->tail->next = heap_create(value, index);
      this->tail = this->tail->next;
      this->size++;
    }
  } else {
    this->head = this->tail = heap_create(value, index);
    this->size++;
  }
}

/**
 * 从序列中弹出长度为minimalLength的区间元素。
 * 若堆在弹出元素后为空，则删除该堆，并合并前后两个堆。
 */
void sequence_pop(Sequence this) {
  Heap heap = NULL;

  // 1、批量弹出minimalLength个果篮的数据。
  for (int times = 0; times < this->minimalLength; times++) {
    for (heap = this->head; heap != NULL; heap = heap->next) {
      int fruit = heap_pop(heap) + 1;
      printf("%d%c", fruit, heap->next != NULL? ' ': '\n');
    }
  }

  // 2、移除长度为0的空的堆。
  // 2.1、找到第一个非空的堆。
  heap = this->head;
  while (heap != NULL && heap_is_empty(heap)) {
    Heap current = heap;
    heap = heap->next;
    heap_delete(current);
  }
  this->head = heap;
  // 2.2、移除剩余空的堆。
  while (heap != NULL && heap->next != NULL) {
    while (heap->next != NULL && heap_is_empty(heap->next)) {
      Heap current = heap->next;
      heap->next = heap->next->next;
      heap_delete(current);
    }
    heap = heap->next;
  }

  // 3、合并邻的水果类型相同的堆。
  for (heap = this->head; heap != NULL && heap->next != NULL; heap = heap->next) {
    while (heap->next != NULL && heap->value == heap->next->value) {
      Heap next = heap->next;
      heap->next = heap->next->next;
      heap_merge(heap, next);
    }
  }

  // 4、计算当前堆最小的长度；堆数量；末尾指针。
  this->minimalLength = this->head != NULL? this->limit: 0;
  this->size = 0;
  for (heap = this->head; heap != NULL; heap = heap->next) {
    if (this->minimalLength > heap->length) {
      this->minimalLength = heap->length;
    }
    this->size++;
    this->tail = heap;
  }
}

/**
 * 从标准输入中加载序列信息。
 */
Sequence sequence_load() {
  Sequence sequence = sequence_create();
  scanf("%d", &sequence->limit);

  int fruit;
  for (int index = 0; index < sequence->limit; index++) {
    scanf("%d", &fruit);
    sequence_push(sequence, index, fruit);
  }

  return sequence;
}

int main(int argc, char* argv[]) {
  Sequence sequence = sequence_load();
  while (!sequence_is_empty(sequence)) {
    sequence_pop(sequence);
  }
  sequence_free(sequence);

  return EXIT_SUCCESS;
}
